import { getNews, getNewsDetail, likeNews, postContent, reqAllPostList, reqPostDetail, likePost, reply, likeComment, deletePost } from '@/api'

const state = {
    newsList: [],
    newsDetail: {},
    postList: [],
    postDetail: {}
};
const mutations = {
    GETNEWSDATA(state, newsList) {
        state.newsList = newsList
    },
    GETNEWSDETAIL(state, newsDetail) {
        state.newsDetail = newsDetail
    },
    GETPOSTLIST(state, postList) {
        state.postList = postList
    },
    GETPOSTDETAIL(state, postDetail) {
        state.postDetail = postDetail
    }
};
const actions = {
    // 资讯
    async getNewsData({ commit }) {
        let res = await getNews();

        if (res.code == 200) {
            commit('GETNEWSDATA', res.data)
        }
    },
    // 资讯详情
    async getNewDetail({ commit }, newsInfo) {
        const { newsId, username } = newsInfo
        let res = await getNewsDetail({ id: newsId, username: username })
        if (res.code == 200) {
            commit('GETNEWSDETAIL', res.obj)
            return 'ok'
        } else {
            return Promise.new.Error('faile')
        }
    },
    // 点赞资讯
    async getlikeNews({ commit }, newsInfo) {
        const { newsId, username } = newsInfo
        let res = await likeNews({ id: newsId, username: username });
        if (res.code == 200) {

            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 发帖
    async getPost({ commit }, forumInfo) {
        const { username, content_post, url } = forumInfo;

        let res = await postContent({ username, content_post, url })
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile')
        }
    },
    // 全部帖子
    async getPostList({ commit }) {
        let res = await reqAllPostList();

        if (res.code == 200) {
            commit('GETPOSTLIST', res.data)
            return 'ok'
        } else {
            return Promise.new.Error('faile')
        }

    },
    // 帖子详情页
    async getDetailPost({ commit }, postInfo) {
        const { username, contentid } = postInfo
        let res = await reqPostDetail({ username: username, contentid: contentid })
        if (res.code == 200) {
            commit('GETPOSTDETAIL', res.obj);
            return 'ok'
        } else {

            return Promise.new.Error('faile')

        }

    },
    // 点赞帖子
    async getlikePost({ commit }, postInfo) {
        const { contentid, username } = postInfo
        let res = await likePost({ username: username, contentid: contentid });
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 评论帖子
    async replyPost({ commit }, replyInfo) {
        const { content_id, follow_username, follow_content_post } = replyInfo;
        let res = await reply({ content_id, follow_username, follow_content_post })
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 点赞评论
    async getLikeComment({ commit }, commentInfo) {
        const { f_id, username } = commentInfo

        let res = await likeComment({ username: username, f_id: f_id });
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    },
    //删除自己的帖子
    async deletemyPost({ commit }, postInfo) {
        const { content_id } = postInfo
        let res = await deletePost({ content_id });
        if (res.code == 200) {
            return '删帖成功'
        } else {
            return Promise.new.Error('faile');
        }
    }
};
const getters = {};

export default {
    state,
    mutations,
    actions,
    getters
}